import { ApiExtraModels, ApiProperty, getSchemaPath } from "@nestjs/swagger";
import { CrudValidationGroups } from "@nestjsx/crud";
import { IsNumber, IsOptional, IsString, IsDecimal, IsDate } from "class-validator";
import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import { BaseEntity } from "./base.entity";
import { SubsidyInfo } from "./subsidy-info.entity";
import { SubsidyType } from "./subsidy-type.entity";
import { Village } from "./village.entity";
import { Group } from "./group.entity";

const { CREATE, UPDATE } = CrudValidationGroups;


@Entity()
export class Subsidy extends BaseEntity {

  @ApiProperty({
    description: '补助项主键',
    required: false
  })
  @PrimaryGeneratedColumn()
  id: number;

  @ApiProperty({
    example: 1,
    description: '所属村',
    required: true,
    oneOf: [
      {
        type: 'Number'
      },
      {
        $ref: getSchemaPath(() => Village)
      }
    ]
  })
  @ManyToOne(() => Village, Village => Village.subsidy)
  village: Number | Village


  @ApiProperty({
    example: 1,
    description: '补助项类型',
    required: true,
    oneOf: [
      {
        type: 'Number'
      },
      {
        $ref: getSchemaPath(() => SubsidyType)
      }
    ]
  })
  @ManyToOne(() => SubsidyType, SubsidyType => SubsidyType.subsidy)
  type: Number | SubsidyType

  @ApiProperty({
    example: '补助类型 -名字',
    description: '补助类型 -名字',
    required: true
  })
  @IsString({ always: true })
  @Column({
    type: "char",
    length: 255,
  })
  name: string

  @ApiProperty({
    example: '补助项 - 补助金额(前端一定要展示单位)',
    description: '补助项 - 补助金额',
    required: true
  })
  // @IsDecimal({}, { always: true })
  // @IsNumber()
  @IsOptional()
  @Column({
    type: 'decimal',
    precision: 14,
    scale: 2,
    zerofill: false,
    nullable: true
  })
  amount: number

  // nationalPolicy: enum   
  // implementationPolicy: enum 

  @ApiProperty({
    example: '补助项 -物品',
    description: '补助项 -物品',
    required: true
  })
  @IsString({ always: true })
  @IsOptional({ always: true })
  @Column({
    type: "char",
    length: 255,
    nullable: true
  })
  items: string

  @ApiProperty({
    example: '补助项 -标准',
    description: '补助项 -标准',
    required: true
  })
  @IsString({ always: true })
  @IsOptional({ always: true })
  @Column({
    type: "char",
    length: 255,
    nullable: true
  })
  standards: string

  @ApiProperty({
    example: '补助项 -申请时间',
    description: '补助项 -申请时间',
    required: true
  })
  @IsString({ always: true })
  @IsOptional({ always: true })
  @Column({
    type: "date",
    nullable: true
  })
  applicationTime: Date

  @ApiProperty({
    example: '补助项 -领取时间',
    description: '补助项 -领取时间',
    required: true
  })
  @IsString({ always: true })
  @IsOptional({ always: true })
  @Column({
    type: "date",
    nullable: true
  })
  pickUpTime: Date

  @ApiProperty({
    example: '相关国家政策',
    description: '相关国家政策',
    required: true
  })
  @IsString({ always: true })
  @IsOptional({ always: true })
  @Column({
    type: "text",
    nullable: true
  })
  nationalPolicy: string

  @ApiProperty({
    example: '实施政策',
    description: '实施政策',
    required: true
  })
  @IsString({ always: true })
  @IsOptional({ always: true })
  @Column({
    type: "text",
    nullable: true
  })
  implementationPolicy: string
  
  @ApiProperty({
    example: 1,
    description: '补助信息',
    required: false,
    type: Number
  })
  @OneToMany(() => SubsidyInfo, SubsidyInfo => SubsidyInfo.subsidy, {
    cascade: ["remove"]
  })
  subsidyInfo : SubsidyInfo []
}